package com.xxx;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.util.Iterator;

/**
 * 服务端流式响应通信方式
 * 同步方式
 */
public class GrpcClient3 {
    public static void main(String[] args) {
        // 1.创建通信的管道
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000).usePlaintext().build();
        try {
            // 2.获取代理对象 stub
            HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub = HelloServiceGrpc.newBlockingStub(managedChannel);
            // 3.完成RPC调用
            // 3.1准备参数
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("xxx");
            // 3.2进行功能RPC调用 获取响应内容
            Iterator<HelloProto.HelloResponse> helloResponseIterator = helloServiceBlockingStub.c2ss(builder.build());
            while (helloResponseIterator.hasNext()) {
                HelloProto.HelloResponse helloResponse = helloResponseIterator.next();
                System.out.println("result = " + helloResponse.getResult());
            }
        } catch (Exception e) {
            throw new RuntimeException();
        } finally {
            // 关闭通道
            managedChannel.shutdown();
        }
    }
}
